clc
clear all
close all
%************constants
L = 10;
N = 128;
N2 = N / 2;
multi =  2 * pi * L; 
filterLen = 1;

%************initial 
w = zeros(N,N);
wave =  dbaux(filterLen);
[lpd,hpd,lpr,hpr] = orthfilt(wave); 
Fnh = createFn(N2);
blkdiag(Fnh);
fnd = blkdiag(Fnh,Fnh);

for i=1:N
    for j = 1:N
       w(i,j) = sqrt( (multi * (j-N2))^2 + (multi * (i-N2))^2);  
    end
end
%w = fftshift(w);
omega = @(t,n) exp(-1i * w(1:n,1:n) *t);

%x =  fftshift( full(createPsiK2D([2 2],[0.125 0.125],[0 0],L,N)));
x = full(createPsiK2D([8 0],[0.05 0.05],[5 4],L,N));
t = 1;


[A B C D] = getTwiddleiFFT(N,filterLen);
X = [diag(A) diag(B); diag(C) diag(D)];

t = 0;
f = figure;
while true
    if ishandle(f)
        t = t + 0.001;
      


         next = x;%.*omega(t,N);  
        [ll hl lh hh] = dwt2(next,lpd,hpd,'mode','per');
        [al bl cl dl] = dwt2(omega(t,N),lpd,hpd,'mode','per');
        ze = zeros(N2,N2);
       % lh = lh.*(cl);
       % hl = hl.*(bl);
       % hh = hh.*(dl);
       % ll = ll.*(al);
        lh = lh.*(omega(t,N2));
        hl = hl.*(omega(t,N2));
        hh = hh.*(omega(t,N2));
        ll = ll.*(omega(t,N2));
        fa = ifft(ifft(ll.').');
        fh = ifft(ifft(hl.').');
        fv = ifft(ifft(lh.').');
        fd = ifft(ifft(hh.').');
        %fh = fft2(hl);
        %fv = fft2(lh);
        %fd = fft2(hh);

        wtOrigin = [ll lh;hl hh];
        wt = [fa fv;fh fd];



        rec = (X * (X * wt).').';
        imagesc(abs(rec));
        drawnow
    else
        break;
    end
end
frec = ifft(ifft(x.').');

subplot(3,1,1);
imagesc(abs(frec));

subplot(3,1,2);
imagesc(abs(rec));

subplot(3,1,3);
imagesc(abs(X));

mwt = X * fnd*(X*fnd*wtOrigin.').';
imagesc(abs(mwt));
